import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [{
  name: 'goods/:id',
  component: () =>
      import('@/view/goods/index')
},
{
  name: 'token',
  component: () =>
      import('@/view/token/index')
},
{
  name: 'cart',
  component: () =>
      import('@/view/cart'),
  meta: {
    title: '购物车'
  }
},
{
  name: 'log',
  component: () =>
      import('@/view/log'),
  meta: {
    title: '获取记录'
  }
},
{
  name: 'help',
  component: () =>
      import('@/view/help'),
  meta: {
    title: '帮助'
  }
},
{
  name: 'helpdetail/:id',
  component: () =>
      import('@/view/help/detail'),
  meta: {
    title: '帮助'
  }
},
{
  name: 'createorder',
  component: () =>
      import('@/view/createorder'),
  meta: {
    title: '确认订单'
  }
},
{
  name: 'orderlist',
  component: () =>
      import('@/view/orderlist'),
  meta: {
    title: '我的订单'
  }
},
{
  name: 'orderdetail',
  component: () =>
      import('@/view/orderdetail'),
  meta: {
    title: '订单详情'
  }
}
]

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '')
})

const router = new Router({
  routes: routes
})

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
    document.title = title
  }
  next()
})

export default router
